package david.java.tools;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: ZhaoDawei
 * @Date: Create in 8:16 下午 2022/7/20
 */
public class ViewCreater extends BaseUtil {


    static String input = "src/tmp/src_view";
    static String output = "target-tmp/tmp-view";


    public static void main(String[] args) throws IOException {
        mkdirIfNotExists(output);
        extract(input, output);

    }


    private static void extract(String input, String output) throws IOException {
        extract(input, output, false, true);
    }

    private static String backQuote(String str) {
        return "`" + str + "`";
    }

    private static void extract(String input, String output, boolean horizontal, boolean backQuote) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(input));
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        List<String> result = new ArrayList<>();
        List<String> annotation = new ArrayList<>();
        int maxLength = 0;

        String line;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isNotBlank(line)) {
                String[] split = line.split("\\s");
                inner:
                for (String str : split) {
                    // 过滤注释
                    if (str.startsWith("--")) {
                        break inner;
                    }
                    if (StringUtils.isNotBlank(str)) {
                        char[] chars = str.toCharArray();
                        StringBuilder sb = new StringBuilder();
                        for (char c : chars) {
                            if (c != '`' && c != ',') {
                                sb.append(c);
                            }
                        }
                        if (backQuote) {
                            result.add(backQuote(sb.toString()));
                            maxLength = Math.max(maxLength, sb.toString().length());
                        } else {
                            result.add(sb.toString());
                            maxLength = Math.max(maxLength, sb.toString().length());
                        }
                        break inner;
                    }
                }

                String[] comments = line.split("\\scomment\\s");
                String comment = "'',";
                if (comments.length == 1) {
                    comments = line.split("\\sCOMMENT\\s");
                }

                if (comments.length == 2) {
                    comment = comments[1];
                }

                annotation.add(comment);
            }
        }


        bw.write("DROP VIEW IF EXISTS `$target.table` ;");
        bw.newLine();
        bw.write("CREATE VIEW IF NOT EXISTS `$target.table` (");
        bw.newLine();

        for (int i = 0; i < result.size(); i++) {
            if (i == 0) {
                if (annotation.get(i).endsWith(",")) {
                    bw.write("\t " + result.get(i) + blankFilter(result.get(i), maxLength) + "COMMENT\t\t" + annotation.get(i).substring(0, annotation.get(i).length() - 1));
                } else {
                    bw.write("\t " + result.get(i) + blankFilter(result.get(i), maxLength) + "COMMENT\t\t" + annotation.get(i));
                }
            } else {
                bw.write("\t,");
                if (annotation.get(i).endsWith(",")) {
                    bw.write(result.get(i) + blankFilter(result.get(i), maxLength) + "COMMENT\t\t" + annotation.get(i).substring(0, annotation.get(i).length() - 1));
                } else {
                    bw.write(result.get(i) + blankFilter(result.get(i), maxLength) + "COMMENT\t\t" + annotation.get(i));
                }
            }
            bw.newLine();
        }
        bw.write(") COMMENT \"\"-- todo fill in the table comment here!!!");
        bw.newLine();
        bw.write("AS");
        bw.newLine();
        bw.write("SELECT");

        bw.flush();
        br.close();
        bw.close();
    }

}
